//transmit support for std::set
//$Id: transmit_set.h 480 2010-05-12 17:21:55Z Oleg.Bulychov $
#pragma once

#include <set>
#include "src/heo/include/transmit.h"
//__________________________________________________________________________________
namespace meta
{

template<class X, class P, class A>
struct is_visitable< std::set<X, P, A> >
{
    enum {result = 0};
};

}
//__________________________________________________________________________________
template<class X, class P, class A, class V>
struct transmit_handler<std::set<X, P, A>, V>
{
    static void visit(std::set<X, P, A>& s, V& v)
    {
        visit(s, v, meta::int2type<V::transmit_tag>());
    }

    static void visit(std::set<X, P, A>& s, V& bcast, meta::int2type<BCAST_TAG>)
    {
        if (bcast.is_Root())
        {
            visit(s, bcast, meta::int2type<SEND_TAG>());
        }
        else
        {
            visit(s, bcast, meta::int2type<RECV_TAG>());
        }
    }

    static void visit(std::set<X, P, A>& s, V& send, meta::int2type<SEND_TAG>)
    {
        size_t cb = s.size();
        //std::cout << "send s::size==" << cb << std::endl;
        send << cb;
        if (cb > 0)
        {
            //size_t i = 0;
            for (typename std::set<X, P, A>::iterator it = s.begin(); it != s.end(); ++it)
            {
                //std::cout << "send s[" << i << "]==" << *it << std::endl;
                send << (X&)*it;
                //++i;
            }
        }
    }

    static void visit(std::set<X, P, A>& s, V& recv, meta::int2type<RECV_TAG>)
    {
        size_t cb = 0;
        //std::cout << "try recv s::size" << std::endl;
        recv >> cb;
        //std::cout << "recv s::size==" << cb << std::endl;
        for (size_t i = 0; i < cb; ++i)
        {
            //std::cout << "try recv s[" << i << "]" << std::endl;
            X x;
            recv >> x;
            //std::cout << "recv s[" << i << "]==" << x << std::endl;
            s.insert(x);
        }
    }
};
//__________________________________________________________________________________
//__________________________________________________________________________________

//EOF!
